import {questionData} from '@/static/js/quickly/tcm/qusetionnaireDetail.js';
import { getUrlParam} from '@/static/js/filter.js';
export default{
	data() {
		return {
			currentIndex: 0,
			questionData:JSON.parse(JSON.stringify(questionData)),
			nowType:'平和质',
			currentPageNo: 1,  //设置分页
			questionArr:[], //tab标题
			sex: '',
			id: '',
			current: '',
			showModal: false,
			showContent: '',
		}
	},
	onLoad() {
		this.current = getUrlParam('current');
		this.id = getUrlParam('userId'); //用户填写了问卷的
		this.sex = getUrlParam('sex');
		this.editId = getUrlParam('editId');
		this.sex == 2 ? this.questionData[5].data.splice(6,1): this.questionData[5].data.splice(5,1);
		this.currentIndex = this.current || 0;
		if(!getUrlParam('between')) {		
			if(this.editId && !localStorage.getItem('questionData')) {
				if(getUrlParam('editId')) {
					const id = getUrlParam('editId')
					this.$api.findByConstitutionId(id).then((res) => {
						this.questionData.map((a,index)=>{
							res.data.result && res.data.result[a.name]  && res.data.result[a.name].map((b,bindex)=>{
								a.data[bindex] && a.data[bindex].options.map((c)=>{
									c.id = b.id
									if(c.userOption == b.userOption){
										c.checked = true
									}
								})
							})
						})
					})
				}
				// this.$api.toBbtainTCM(this.id,{userId:this.id}).then((res) => {
				// 	this.questionData.map((a,index)=>{
				// 		res.data.result && res.data.result[a.name]  && res.data.result[a.name].map((b,bindex)=>{
				// 			a.data[bindex] && a.data[bindex].options.map((c)=>{
				// 				c.id = b.id
				// 				if(c.userOption == b.userOption){
				// 					c.checked = true
				// 				}
				// 			})
				// 		})
				// 	})
				// })
			}
		}else {
			if(localStorage.getItem('questionData')){
				// 从详情页点击回来查看选中的详情
				if(this.current) {
					const questionHistory = localStorage.getItem('questionData') && JSON.parse(localStorage.getItem('questionData'));
					this.questionData = questionHistory;
					this.questionArr = this.questionData[this.currentIndex].data;
				} else {
					this.questionArr = this.questionData[0].data;
					this.questionArr = this.questionData.filter(a=>{return a.name == this.nowType})[0].data;
				}
			}
		}
	},
	mounted(){		
		if(!this.current) {
			this.questionArr = this.questionData[0].data;			
		} else {
			this.questionArr = this.questionData[this.currentIndex].data;
		}
	},
	methods: {
		// tab切换
		switchClick(name,index){
			uni.pageScrollTo({
				 scrollTop: 0,
				 duration: 0
			});
			this.currentPageNo = 1;
			this.nowType = name;
			this.currentIndex = index;
			this.questionArr = this.questionData.filter(a=>{return a.name == name})[0].data;
			if(!getUrlParam('between')) {
				// if(this.id) {
				// 	this.$api.toBbtainTCM(this.id,{userId:this.id}).then((res) => {
				// 		this.questionData.map((a,index)=>{
				// 			res.data.result && res.data.result[a.name] && res.data.result[a.name].map((b,bindex)=>{
				// 				a.data[bindex] && a.data[bindex].options.map((c)=>{
				// 					c.checked = false
				// 					c.checked2 = false
				// 					c.id = b.id
				// 					if(c.userOption == b.userOption){
				// 						c.checked = true
				// 						c.checked2 = true
				// 					}
				// 					if(b.healthOption == c.userOption){
				// 						c.checked2 = true
				// 					}
				// 				})
				// 			})
				// 		})
				// 	})
				// }
				if(getUrlParam('editId')) {
					const id = getUrlParam('editId')
					this.$api.findByConstitutionId(id).then((res) => {
						this.questionData.map((a,index)=>{
							res.data.result && res.data.result[a.name] && res.data.result[a.name].map((b,bindex)=>{
								a.data[bindex] && a.data[bindex].options.map((c)=>{
									c.checked = false
									c.checked2 = false
									c.id = b.id
									if(c.userOption == b.userOption){
										c.checked = true
										c.checked2 = true
									}
									if(b.healthOption == c.userOption){
										c.checked2 = true
									}
								})
							})
						})
					})
				}
				
			}
		},
		// 判定跳转
		determine() {
			if(this.id && this.editId && !getUrlParam('userStatus')) {
				uni.navigateTo({
					url:'/pages/quickly/tcm/physical2?userId='+this.id+'&editId='+this.editId+'&status='+getUrlParam('status')+'&mark='+getUrlParam('mark')+'&sex='+getUrlParam('sex')
				})
			} else if(this.id && this.editId && getUrlParam('userStatus')) {
				uni.navigateTo({
					url:'/pages/quickly/tcm/physical2?userId='+this.id+'&editId='+this.editId+'&sex='+getUrlParam('sex')+'&userStatus=1'
				})
			}
			// if(this.id  && !this.editId) {
			// 	console.log(this.editId)
			// 	uni.navigateTo({
			// 		url:'/pages/quickly/tcm/physical2?userId='+this.id+'&status='+getUrlParam('status')+'&mark='+getUrlParam('mark')+'&sex='+getUrlParam('sex')+'&between='+1
			// 	})
			// }
			
			this.filterResult = this.questionData.map((a)=>{
				const bresult = a.data.filter((b)=>{
					b.nullchecked = false
					const cresult = b.options.filter((c)=>{
						return c.checked2
					})
					if(cresult.length <= 0){
						b.nullchecked = true
					}
					return b.nullchecked;
				})
				a.bresult = bresult
				return a
			})
			this.i = 0;
			this.showToast(this.filterResult);
		},
		showToast(filterResult){
			if(filterResult.length > 0){
				if(filterResult[this.i]){
					if(filterResult[this.i].bresult.length > 0){
						this.showModal = true,
						this.showContent = '请选择'+filterResult[this.i].name+'底下的'+filterResult[this.i].bresult[0].title
						// uni.showToast({title:'请选择'+filterResult[this.i].name+'底下的'+filterResult[this.i].bresult[0].title,icon:'none'})
					}else{
						this.i++
						this.showToast(this.filterResult);
					}
				}else{
					if(this.id && this.editId && !getUrlParam('userStatus')) {
						uni.navigateTo({
							url:'/pages/quickly/tcm/physical2?userId='+this.id+'&editId='+this.editId+'&status='+getUrlParam('status')+'&mark='+getUrlParam('mark')+'&sex='+getUrlParam('sex')
						})
					}else if (this.id && this.editId && getUrlParam('userStatus')) {
						uni.navigateTo({
							url:'/pages/quickly/tcm/physical2?userId='+this.id+'&editId='+this.editId+'&sex='+getUrlParam('sex')+'&userStatus=1'
						})
					}else {
						uni.navigateTo({
							url:'/pages/quickly/tcm/physical2?userId='+this.id+'&status='+getUrlParam('status')+'&mark='+getUrlParam('mark')+'&sex='+getUrlParam('sex')+'&between='+1
						})
					}
				}
			}
		},
		Modalconfirm() {
			this.showModal = false;
		},
			// 上一页
		prevPage(){
			uni.pageScrollTo({
				 scrollTop: 0,
				 duration: 0
			});
			if(this.currentIndex == 0){
				uni.showToast({title:'这已经是第一页了~',icon:'none'})
				return;   
			}
			this.currentIndex--;
			this.questionArr = this.questionData[this.currentIndex].data;
		},
		// 下一页
		nextPage() {
			const _this = this;  
			uni.pageScrollTo({
				 scrollTop: 0,
				 duration: 0
			});
			if(_this.currentIndex == 8) {
				_this.determine();
				return; 
			}
			_this.currentIndex++;          
			_this.questionArr = _this.questionData[_this.currentIndex].data
		},
		// 选答案
		selectedTitle(aitem,item) {
			if(this.id && getUrlParam('mark') == 2 || getUrlParam('userStatus')){
				return                    
			} else {
				item.options.map((a)=>{a.checked2 = false})
				aitem.checked2 = true
				localStorage.setItem('questionData',JSON.stringify(this.questionData))
			}
		}
	}
}